﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class OperationImplementLineAdd : OperationImplement
{

    IList<SimpleVoxel> oldSerializeVoxel = new List<SimpleVoxel>();
    Vector3Int rectangleOldPosition;
    //public OperationImplementLineAdd() : base()
    //{
        
    //}

    public override void Start(StartArg arg)
    {
        base.Start(arg);
        rectangleOldPosition = arg.StartPosition;
    }

    public override void Run(RunArg arg)
    {
        if (rectangleOldPosition != arg.nowSelectPosition)
        {
            Vector3Int positionA = arg.gestureStartPosition;
            rectangleOldPosition = arg.nowSelectPosition;

            List<Voxel3D> addArray = BasicAlgorithm.GetLineBlock3D(positionA, rectangleOldPosition, arg.nowSelectUVKey);

            Vector3Int[] arrayPoint = new Vector3Int[oldSerializeVoxel.Count];
            for (int i = 0, max = oldSerializeVoxel.Count; i < max; i++)
            {
                arrayPoint[i] = oldSerializeVoxel[i].position;
            }
            nowDrawVoxel3DCore.DrawRemoveBlock(arrayPoint);
            oldSerializeVoxel = nowDrawVoxel3DCore.DrawAddBlock(addArray);
        }
    }

    public override void Over(OverArg arg)
    {
        if (isStop)
        {
            if (oldSerializeVoxel.Count > 0)
            {
                proxy.Write(VoxelCategories.Operation.Add, historyRecordFrontList, oldSerializeVoxel);
                oldSerializeVoxel = new List<SimpleVoxel>();
            }
        }
        else
        {
            if (oldSerializeVoxel.Count > 0)
            {
                proxy.Write(VoxelCategories.Operation.Add, historyRecordFrontList, oldSerializeVoxel);
                oldSerializeVoxel = new List<SimpleVoxel>();
            }
            else
            {

                oldSerializeVoxel = nowDrawVoxel3DCore.DrawAddBlock(new Voxel3D[] { new Voxel3D(arg.nowSelectPosition, arg.nowSelectUVKey) });
                if (oldSerializeVoxel.Count > 0)
                {
                    proxy.Write(VoxelCategories.Operation.Add, historyRecordFrontList, oldSerializeVoxel);
                    oldSerializeVoxel = new List<SimpleVoxel>();
                }
            }
        }
    }

}
